package com.controller;

import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Paper;
import com.entity.Paper_Question;
import com.entity.Question;
import com.entity.Users;
import com.service.PaperService;
import com.service.QuestionService;

@Controller
@RequestMapping("/paper")
public class PaperController {
	
	@Resource
	PaperService ps;

	
	
	@ResponseBody
	@RequestMapping("/list")
	public Map list(@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="5") int rows,Paper p){
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("pageStart", (page-1)*rows);
		m.put("pageSize", rows);
		m.put("param",p);
		m = ps.list(m);
		return m;
	}
	
	@ResponseBody
	@RequestMapping("/get")
	public Paper get(@RequestParam(required=true) int id){
		Paper p = ps.get(id);
		return p;
	}
	
	@ResponseBody
	@RequestMapping(value="/add",produces="text/html;charset=utf-8")
	public String add(Paper p,int[] qid,HttpSession session ){
		Users u = (Users) session.getAttribute("user");
		p.setCreateuserid(u.getUserid());
		if(ps.add(p,qid)){
			return "新增试卷成功！";
		}
		return "新增失败，请重试！";
	}
	
	
	@ResponseBody
	@RequestMapping(value="/edit",produces="text/html;charset=utf-8")
	public String edit(Paper p,int[] qid,HttpSession session ){
		Users u = (Users) session.getAttribute("user");
		p.setCreateuserid(u.getUserid());
		if(ps.edit(p,qid)){
			return "修改成功！";
		}
		return "修改失败，请重试！";
	}
	
	@ResponseBody
	@RequestMapping(value="/delete",produces="text/html;charset=utf-8")
	public String delete(@RequestParam(required=true) int id){
		if(ps.delete(id)){
			return "删除成功！";
		}
		return "删除失败，请重试！";
	}
}
